package cn.tedu.spotms.server.controller;

import cn.tedu.spotms.server.pojo.dto.ProjectAddNewDTO;
import cn.tedu.spotms.server.pojo.dto.ProjectUpdateInfoDTO;
import cn.tedu.spotms.server.pojo.vo.ProjectSimpleVO;
import cn.tedu.spotms.server.service.IProjectService;
import cn.tedu.spotms.server.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/projects" , produces = "application/json;charset=utf-8")
@Api(tags = "游乐管理模块")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    @PostMapping("/add-new")
    @ApiOperation("添加游乐项目")
    @PreAuthorize("hasAuthority('/project/update')")
    public JsonResult<Void> addNewProject(ProjectAddNewDTO projectAddNewDTO){
        projectService.addNewProject(projectAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除游乐项目")
    @PreAuthorize("hasAuthority('/project/delete')")
    public JsonResult<Void> deleteProjectById(@PathVariable Long id){
        projectService.deleteProjectById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改游乐项目")
    @PreAuthorize("hasAuthority('/project/update')")
    public JsonResult<Void> updateProjectInfo(ProjectUpdateInfoDTO project){
        projectService.updateProjectInfo(project);
        return JsonResult.ok();
    }

    @GetMapping("/select")
    @ApiOperation("查询单个游乐项目")
    @PreAuthorize("hasAuthority('/project/read')")
    public JsonResult<ProjectSimpleVO> getSimpleByName(String name){
        ProjectSimpleVO project = projectService.getProjectSimpleByName(name);
        return JsonResult.ok(project);
    }

    @GetMapping("/list")
    @ApiOperation("查询所有游乐项目")
    @PreAuthorize("hasAuthority('/project/read')")
    public JsonResult<List<ProjectSimpleVO>> list(){
        List<ProjectSimpleVO> list = projectService.projectList();
        return JsonResult.ok(list);
    }
}
